﻿using System.Web;
using System.Web.Mvc;
using MvcLibrary.Repository;
using System.IO;
using WebVideo.Areas.Admin.Models;

using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Collections;
using CarlosAg.ExcelXmlWriter;
using WebVideo.Controllers;

namespace WebVideo.Areas.Admin.Controllers
{
    public class UserController : Controller
    {
        //
        // GET: /Admin/User/

        public ActionResult Index()
        {

            return View(UserModel.Index());
        }

        #region "Xuat bao cao"
        public JsonResult saveExcel()
        {
            Workbook book = new Workbook();
            ReportController  rp = new ReportController();
            IList<Report> TieuDe = new List<Report>();
            IList<Report> NoiDung = new List<Report>();
            string Name = "Danh sách User";
            dbVideoDataContext db = new dbVideoDataContext();
            // tạo danh sách tiêu đề

            var query = db.EntityUsers.ToList();


            Report k = new Report();
            k.Name = "UserName";
            TieuDe.Add(k);

            k = new Report();
            k.Name = "Name";
            TieuDe.Add(k);

            k = new Report();
            k.Name = "Năm sinh";
            TieuDe.Add(k);

            k = new Report();
            k.Name = "Giới tính";
            TieuDe.Add(k);

            k = new Report();
            k.Name = "Tỉnh/thành phố";
            TieuDe.Add(k);

            k = new Report();
            k.Name = "Email";
            TieuDe.Add(k);

            k = new Report();
            k.Name = "Tên ngân hàng";
            TieuDe.Add(k);

            k = new Report();
            k.Name = "Số tài khoản";
            TieuDe.Add(k);


            k = new Report();
            k.Name = "Link mời bạn";
            TieuDe.Add(k);

            k = new Report();
            k.Name = "Người mời vào website";
            TieuDe.Add(k);

            k = new Report();
            k.Name = "Ip đăng ký";
            TieuDe.Add(k);

            k = new Report();
            k.Name = "Điểm xem video";
            TieuDe.Add(k);

            k = new Report();
            k.Name = "Điểm bạn bè cấp 1";
            TieuDe.Add(k);

            k = new Report();
            k.Name = "Điểm bạn bè cấp 2";
            TieuDe.Add(k);

            k = new Report();
            k.Name = "Tình trạng";
            TieuDe.Add(k);

            int kk = query.Count;
            foreach (var item in query)
            {
                k = new Report();
                k.Name = item.UserName;
                NoiDung.Add(k);

                k = new Report();
                k.Name = item.Name;
                NoiDung.Add(k);

                k = new Report();
                k.Name = item.YearBorn.ToString();
                NoiDung.Add(k);

                k = new Report();
                k.Name = item.Sex;
                NoiDung.Add(k);

                k = new Report();
                k.Name = item.City;
                NoiDung.Add(k);

                k = new Report();
                k.Name = item.Email;
                NoiDung.Add(k);

                k = new Report();
                k.Name = item.NameBank;
                NoiDung.Add(k);

                k = new Report();
                k.Name = item.BankAccount;
                NoiDung.Add(k);

                k = new Report();
                k.Name = item.Link;
                NoiDung.Add(k);

                k = new Report();
                k.Name = item.UserNameRoot;
                NoiDung.Add(k);

                k = new Report();
                k.Name = item.Ip;
                NoiDung.Add(k);

                k = new Report();
                k.Name = item.PointVideo.ToString();
                NoiDung.Add(k);

                k = new Report();
                k.Name = item.PointFriend1.ToString();
                NoiDung.Add(k);

                k = new Report();
                k.Name = item.PointFriend2.ToString();
                NoiDung.Add(k);


                k = new Report();
                if (item.Status == 1)
                    k.Name = "Đang hoạt động";
                else
                    k.Name = "Đang bị ban";
                NoiDung.Add(k);

               
            }


            book = rp.Index(book, TieuDe, NoiDung, Name);
            string name = "User_YeuQuangCao"+ ".xls";
            var path = Path.Combine(Server.MapPath("~/Content/"), name);
            book.Save(path);
            return Json(name);
        }

        #endregion

        #region "Change point"

        public string ChangePoint(string type, string username, string value)
        {
            try
            {
               int va = int.Parse(value);
                dbVideoDataContext db = new dbVideoDataContext();
                EntityUser us = db.EntityUsers.Where(p => p.UserName == username).First();
                if (type == "1")
                    us.PointVideo = va;
                else if (type == "2")
                    us.PointFriend1 = va;
                else if (type == "3")
                    us.PointFriend2 = va;
                db.SubmitChanges();
                return "Complete";
            }
            catch
            {
                return "error";
            }
           
        }
        #endregion
        #region "Edit"
        public ActionResult Edit(string US)
        {
            return View(UserModel.LayModel(US));
        }
      
        #endregion //Edit
        public ActionResult Ban(string US, int l)
        {
            try
            {
                UserModel.Ban(US, l);
                return RedirectToAction("Index", "User");

            }
            catch
            {
                return RedirectToAction("Index", "Error", new { errorMsg = "Lỗi SQL hosting / server" });

            }

        }
        #region "Delete"

        public string Delete(string Username)
        {
            try
            {
                UserModel.DeleteChat(Username);
                UserModel.DeleteFriend(Username);
                UserModel.DeleteUser(Username);
                UserModel.Delete(Username);
                return "ok";
            }
            catch
            {
                return "error";
            }
        }

        #endregion //Delete
    }
}
